package com.algoshow.controller;



import com.algoshow.DTO.DataDTO;
import com.algoshow.model.*;
import com.algoshow.utils.SystemConstants;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author vvv
 * @date 2023-05-17 12 56
 * discription
 */
public class AlgoController {


    public AlgoController() {
    }

    public void start(String algo, DataDTO dataDTO) {
        String[] algoString = algo.split(":");
        // 算法类型
        String algoType = algoString[0];
        // 算法名称
        String algoName = algoString[1];

        String data = dataDTO.getData();
        Integer index = dataDTO.getIndex();
        Integer value = dataDTO.getValue();
        switch (algoType) {
            case "sort" :
                List<SortData> sortDataList = initSortData(data);
                new SortPage(sortDataList, algoName);
                break;
            case "tree":
                TreeNode root = initTree(data);
                new TreePage(root, algoName);
                break;
            case "list":
                ListNode head = initList(data);
                new ListNodePage(head, index, value, algoName);
                break;
            case "array":
                List<ArrayData> arr = initArray(data);
                new ArrayPage(arr, index, value, algoName);
                break;
            case "stack":
                List<StackNode> stringNode = initStack(data);
                new StackAndQueuePage(stringNode, algoName);
                break;
        }
    }

    private List<StackNode> initStack(String data) {
        List<StackNode> stringNode = new LinkedList<>();
        int x = 500;
        int y = 300;
        //x + i * 50 + 100, y
        for (int i = 0; i < data.length(); i++) {
            StackNode stackNode = new StackNode(data.charAt(i), x + i * 50 + 100, y);
            stringNode.add(stackNode);
        }
        return stringNode;
    }

    private List<ArrayData> initArray(String data) {
        List<ArrayData> list = new ArrayList<>();
        for (int i = 0; i < data.length(); i++) {
            list.add(new ArrayData(data.charAt(i) - '0'));
        }
        return list;
    }

    private ListNode initList(String data) {
        ListNode dummyHead = new ListNode(-1);
        ListNode cur = dummyHead;
        for (int i = 0; i < data.length(); i++) {
            int num = data.charAt(i) - '0';
            ListNode node = new ListNode(num);
            cur.next = node;
            cur = cur.next;
        }
        return dummyHead;
    }

    private List<SortData> initSortData(String data) {

        List<SortData> list = new ArrayList<>();
        for (int i = 0; i < data.length(); i++) {
            int num = data.charAt(i) - '0';
            SortData sortData = new SortData(num, i * 100 + 100, 500);
            list.add(sortData);
        }
        return list;
    }

    private TreeNode initTree(String data) {
        List<TreeNode> tree = new ArrayList<>();
        TreeNode root = null;
        // 初始化每个节点的数据
        for (int i = 0; i < data.length(); i++) {
            TreeNode node = null;
            int num = data.charAt(i) - '0';
            if (data.charAt(i) != 'x') {
                node = new TreeNode(num);
            }
            tree.add(node);
            if (i == 0) {
                root = node;
            }
        }
        // 构造二叉树
        for (int i = 0; i * 2 + 2 < data.length(); i++) {
            if (tree.get(i) != null) {
                tree.get(i).left = tree.get(i * 2 + 1);
                tree.get(i).right = tree.get(i * 2 + 2);
            }
        }
        return root;
    }

    public static void main(String[] args) {

        String inputData = "1234567891234567";

        DataDTO dataDTO = new DataDTO();
        dataDTO.setData(inputData);
        dataDTO.setIndex(3);
        dataDTO.setValue(2);
        // data 格式为 index:value:data
        new AlgoController().start(SystemConstants.SORT_FAST, dataDTO);
        //new AlgoController().start(SystemConstants.SORT_SELECT,dataDTO);
    }
}
